<?php
 include "counter2.php";
 $self_url = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

   $string = $counter->get_input('string', '', 'get');
   $table = $counter->get_input('table', '', 'get');
   $limit = $counter->get_input('limit', 'number', 'get');
   $boolean = $counter->get_input('boolean', '', 'get');
 if (!$limit) {
     $limit = 20;
 } 
pagestart(); ?>
<html>
<?php pagehead(_("Free-text Search for an user")) ?>
<BODY bgcolor="white">
<?php pagetop(_("Free-text Search for an user")) ?>
<?php print _("In free-text search, you get all entries that match at least one word - best match first. If you have read the MySQL manual on boolean freetext searches, click \"Boolean\" and try (+word works).") ?>
<form action="<?php echo $self_url ?>">
<table>
<tr><td><?php print _("String") ?>:<td> <input name="string" value="<?php echo $string ?>">
<tr><td><?php print _("Table") ?>:<td>
<select name="table">
<option value="frozen" <?php if ($table == "frozen") { print "selected"; }?>><?php print _("Frozen") ?>
<option value="persons" <?php if ($table == "persons") { print "selected"; }?>><?php print _("Persons") ?>
</select>
<tr><td><?php print _("Limit") ?>:<td><input name="limit" value="<?php echo $limit ?>">
<tr><td><?php print _("Boolean") ?>:<td><input type="checkbox" name="boolean" value="yes"
<?php if ($boolean) { print "checked"; } # not to be translated! ?>
>
</table>
<input type="submit" value="Search">
<input type="reset" value="Clear">
</form>

<?php
if ($string && $table) {
	if ($boolean) {
	   $booleanmode = 'in boolean mode';
        } else {
           $booleanmode = '';
        }
        if ($table == "frozen") {
          $search = "select users.f_key, users.email, users.state as ustate,
            match(frozen.f_raw) against('$string' $booleanmode) as score
            from users, frozen
            where match(frozen.f_raw) against('$string' $booleanmode)
                and users.f_key = frozen.owner
                and frozen.ftable = 'persons'
               order by score desc limit $limit";
        } else if ($table == "persons") {
          $search = "select users.f_key, users.email,
            persons.name, persons.country, users.state as ustate,
            match(persons.f_raw) against('$string' $booleanmode) as score
            from users, persons
            where match(persons.f_raw) against('$string' $booleanmode)
                and users.f_key = persons.f_key
               order by score desc limit $limit";
        }
        print _("Search expression is") . " \"$search\"<br>\n";
	# Try to figure out how complex this query is, and stop if
	# it's excessively complex
	$complex = mysql_query("explain $search");
	if (!$complex) {
		print mysql_error();
		die("Query for explanation failed");
	}
	$complexity = 0;
	while ($arr = mysql_fetch_array($complex)) {
	     print $arr["table"] . " " . $arr["rows"] . "<br>";
	     $complexity += $arr["rows"];
        }
	if ($complexity > 10000 && $slowsearch != "yes") {
	   print _("This fetches more than 10000 rows. Please specify more information.");
	} else {
	   flush();
	   ob_flush(); # php overkill
 	   $query = mysql_query($search);
	   if (!$query) {
		print mysql_error();
		die("Query failed");
	   }
	   $rows = mysql_num_rows($query);
	   if ($rows == 0) {
		print "<p>\n" . _("Nothing found") . "\n";
	   } else {
		print "<p>\n$rows " . _("entries found") . "\n";
		if ($rows > $limit) {
			print "<br>" . $limit . " " . _("first matches shown") . "\n";
		}
		print "<table border>";
		$countix = 0;
		while ($arr = mysql_fetch_array($query)) {
			print "<tr><td>";
			print "<a href=\"/adm/display-person.php?user=$arr[f_key]\">";
			print $arr[f_key] . "</a>";
			print "<td>" . $arr[email] . "\n";
			print "<td>" . $arr[score] . "\n";

			++ $countix;
			if ($countix > $limit) {
				break;
			}
		}
		print "</table>\n";
	   }
	}
}
?>

<?php pagebottom() ?>
</body>
</html>
